from django.shortcuts import render, redirect, get_object_or_404
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.contrib.auth.decorators import login_required
from .models import InfoData
from .forms import InfoDataForm
import copy
import smartform
from mcom.info_config import TYPE1, TYPE2, getsubtypes

INFO_NUM_OF_PAGE = 10


def type_list(request):
    return render(request, "info/type_list.html", {"objlist":TYPE1.values()})

def subtype_list(request):
    typeid = int(request.GET.get("typeid", "1"))
    return render(request, "info/subtype_list.html", {"objlist":getsubtypes(typeid)})


def latest_info(request):
    info_list = InfoData.objects.all()
    paginator = Paginator(info_list, INFO_NUM_OF_PAGE)
    page = request.GET.get("page")
    try:
        infos = paginator.page(page)
    except PageNotAnInteger:
        infos = paginator.page(1)
    except EmptyPage:
        infos = paginator.page(paginator.num_pages)

    return render(request, "info/latest_info.html", {"objlist":infos})


def info_list(request):
    typeid = int(request.GET.get("typeid"))
    typeinfo = TYPE2[typeid]

    info_list = InfoData.objects.filter(typeid=typeid)
    paginator = Paginator(info_list, INFO_NUM_OF_PAGE)
    page = request.GET.get("page")
    try:
        infos = paginator.page(page)
    except PageNotAnInteger:
        infos = paginator.page(1)
    except EmptyPage:
        infos = paginator.page(paginator.num_pages)

    return render(request, "info/info_list.html", {"objlist":infos, "typeinfo":typeinfo})

@login_required
def add_info(request):
    if request.method == 'POST':
        typeid = int(request.POST.get("typeid"))
        infotype = TYPE2[typeid]
        conf = smartform.getconf(typeid)

        ret, check = smartform.validate(request.POST, conf)
        f = InfoDataForm(request.POST)
        if f.is_valid() and ret:
            info = f.save(False)
            info.detail = smartform.encode(check)
            info.typeid = typeid 
            info.publisher = request.user
            info.save()
            return redirect("/info/info_list?typeid=%d"%(typeid))

        fields = copy.deepcopy(conf["fields"])
        for v in fields:
            n = v["name"]
            if check.has_key(n):
                v["value"] = check[n].get("value", "")
                v["error"] = check[n].get("error", "")

        return render(request, "info/add_info.html", {"form":f, "infotype":infotype, "fields":fields})

    else:
        typeid = int(request.GET.get("typeid"))
        infotype = TYPE2[typeid]
        conf = smartform.getconf(typeid)

        f = InfoDataForm()
        return render(request, "info/add_info.html", {"form":f, "infotype":infotype, "fields":conf["fields"]})


@login_required
def edit_info(request):
    if request.method == 'POST':
        infoid= int(request.POST.get("infoid"))
        info = get_object_or_404(InfoData, pk=infoid)

        infotype = TYPE2[info.typeid]
        conf = smartform.getconf(info.typeid)

        ret, check = smartform.validate(request.POST, conf)
        f = InfoDataForm(request.POST)
        if f.is_valid() and ret:
            info.title = f.cleaned_data["title"]
            info.content = f.cleaned_data["content"]
            info.detail = smartform.encode(check)
            info.save()
            return redirect("/info/info_list?typeid=%d"%(info.typeid))

        fields = copy.deepcopy(conf["fields"])
        for v in fields:
            n = v["name"]
            if check.has_key(n):
                v["value"] = check[n].get("value", "")
                v["error"] = check[n].get("error", "")

        return render(request, "info/edit_info.html", {"form":f, "infotype":infotype, "fields":fields})

    else:
        infoid = int(request.GET.get("infoid"))
        info = get_object_or_404(InfoData, pk=infoid)
        infotype = TYPE2[info.typeid]
        conf = smartform.getconf(info.typeid)
        detail = smartform.decode(info.detail)
        fields = copy.deepcopy(conf["fields"])
        for v in fields:
            n = v["name"]
            if detail.has_key(n):
                v["value"] = detail[n]

        f = InfoDataForm(instance = info)
        return render(request, "info/edit_info.html", {"form":f, "info":info, "fields":fields})


def delete_info(request):
    pass

def set_top(request):
    pass

def flush_info(request):
    pass

